package data;


public class SelectorList<E> extends LinkedList<E>{
	Node<E> selected;
	public SelectorList(){
		super();
		selected = null;
	}
	public void nextTerminal(){
		if(((LinkedList<E>)current.getElement()).isLast() && selected.equals(((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).last)){
			next();
			((LinkedList<E>)current.getElement()).current = ((LinkedList<E>)current.getElement()).first;
			selected = ((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).first;
		}
		else if(selected.equals(((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).last)){
			((LinkedList<E>)current.getElement()).next();
			selected = ((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).first;
		}
		else{
			selected = selected.getNext();
		}
	}
	public void prevTerminal(){
		if(((LinkedList<E>)current.getElement()).isFirst() && selected.equals(((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).first)){
			prev();
			((LinkedList<E>)current.getElement()).current = ((LinkedList<E>)current.getElement()).last;
			selected = ((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).last;
		}
		else if(selected.equals(((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).first)){
			((LinkedList<E>)current.getElement()).prev();
			selected = ((LinkedList<E>)((LinkedList<E>)current.getElement()).current.getElement()).last;
		}
		else{
			selected = selected.getPrev();
		}
	}
	public void nextSelected(){
		selected = selected.getNext();
	}
	public void prevSelected(){
		selected = selected.getPrev();
	}
	public void setSelected(){
		selected = current;
		if(selected == null){
			return;
		}
		if(selected.getElement() instanceof LinkedList<?>){
			setSelected2();
		}
	}
	private void setSelected2(){
		selected = ((LinkedList<E>)selected.getElement()).current;
		if(selected == null){
			return;
		}
		if(selected.getElement() instanceof LinkedList<?>){
			setSelected2();
		}
	}
	public E peekSelected(){
		return selected.getElement();
	}
	public E remove(Object o){
		E element = null;
		int count = this.getTerminalSize();
		
		System.out.println(this.getSize());
		for(int i=0;i<this.getSize();i++)
		{
			((LinkedList<?>)this.peekIndex(i)).remove(o);
		}
		System.out.println("x");
		
		for(int i = 0; i < count; i++){
			if(selected.getElement().equals(o)){
				LinkedList<E> list = selected.getList();
				element = list.remove(o);
				if(list.isEmpty()){
					//Empty All
					if(isEmptyTerminal()){
						selected = null;
					}
					//End of Mode
					if(((LinkedList<E>)current.getElement()).isEmptyTerminal()){
						next();
					}
					//End of Type
					else{
						((LinkedList<E>)current.getElement()).next();
					}
					
					setSelected();
					return element;
				}
				nextTerminal();
			}
			else{
				nextTerminal();
			}
		}
		return element;
	}	
}
